cap cd "/Users/kevincroke/Dropbox/UG Uwezo/Dataverse"

set more off
set varabbrev off

use "constructed_data/for_analysis", clear

global outcomes "st_math st_english st_total"
global attend "inschool neverenrolled"
global controls "i.age##i.rd##female"

*create variable for no access to water
gen no_water=1-water

*************
*Table S1 
*************

*Panel A
foreach var in $outcomes {
reg `var' treat $controls if age1_7==1 & no_water==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls if age1_7==1 & mother_no_edu==1, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S1 Panel A")

*Panel B
foreach var in $outcomes {
reg `var' treat no_water $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat mother_no_edu $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S1 Panel B")

*Panel C
foreach var in $outcomes {
reg `var' treat  $controls $wealth  if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat  $controls private if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S1 Panel C")

*Panel D
foreach var in $outcomes {
reg `var' treat district_dum* if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls district_dum* if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S1 Panel D")


*****
*Table S2
*****

*2010-2011 only
foreach var in $outcomes {
reg `var' treat if age1_7==1 & rd<2012, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls if age1_7==1 & rd<2012, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S2 Panel A")

*2012 only
foreach var in $outcomes {
reg `var' treat if age1_7==1 & rd==2012, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls if age1_7==1 & rd==2012, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S2 Panel B")

*2013 only
foreach var in $outcomes {
reg `var' treat if age1_7==1 & rd==2013, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls if age1_7==1 & rd==2013, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S2 Panel C")

*2014 only
foreach var in $outcomes {
reg `var' treat if age1_7==1 & rd==2014, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls if age1_7==1 & rd==2014, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S2 Panel D")

*2015 only
foreach var in $outcomes {
reg `var' treat if age1_7==1 & rd==2015, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls if age1_7==1 & rd==2015, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S2 Panel E")

***************************************
*Table S3: what are the correlates of missingness? 
***************************************

*generate table on missing values (Appendix Table 1)
reg missing_obs treat if age1_7==1, r cluster(parishcode1) // 
estimates store reg1
reg missing_obs treat i.age i.rd female if age1_7==1, r cluster(parishcode1) // 
estimates store reg2
reg missing_obs treat i.age i.rd female district_dum* if age1_7==1, r cluster(parishcode1) // 
estimates store reg3
reg missing_obs treat i.age i.rd female district_dum* $wealth if age1_7==1, r cluster(parishcode1) // 
estimates store reg4
esttab reg1 reg2 reg3 reg4, compress se starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
  replace title("non-response")


******************************************
*Appendix Table S5: Interactions and attendance
******************************************

*Panel A
foreach var in $attend {
reg `var' treat_female treat female if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat_female treat female $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab inschool inschool1 neverenrolled neverenrolled1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat_female treat female)  replace title("S5 Panel A")

*Panel B
foreach var in $attend {
reg `var' spillover_treat treat others_treated if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' spillover_treat treat others_treated $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab inschool inschool1 neverenrolled neverenrolled1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(spillover_treat treat others_treated)  replace title("S5 Panel B")

*Panel C
foreach var in $attend {
reg `var' treat_poor treat bottom_2_2015 if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat_poor treat bottom_2_2015  $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab inschool inschool1 neverenrolled neverenrolled1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat_poor treat bottom_2_2015 )  replace title("S5 Panel C")

*Panel D
foreach var in $attend {
reg `var' treat_under14 treat under14 if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat_under14 treat under14  $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab inschool inschool1 neverenrolled neverenrolled1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat_under14 treat under14 )  replace title("S5 Panel D")

*Panel E
foreach var in $attend {
reg `var' treat_expose2_4 treat expose2_4, robust cluster (parishcode1) 
est store `var'
reg `var' treat_expose2_4 treat expose2_4  $controls, robust cluster (parishcode1) 
est store `var'1
}
esttab inschool inschool1 neverenrolled neverenrolled1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat_expose2_4 treat expose2_4)  replace title("S5 Panel E")

*Panel F
foreach var in $attend {
reg `var' age1_treat start1 treat years_treated if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' age1_treat start1 treat $controls years_treated if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab inschool inschool1 neverenrolled neverenrolled1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(age1_treat start1 treat years_treated)  replace title("S5 Panel F")

********
*Table S6
********

*main regressions weighted
foreach var in $outcomes {
reg `var' treat [aw=weight] if age1_7==1, robust cluster (parishcode1) 
est store `var'
reg `var' treat $controls [aw=weight] if age1_7==1, robust cluster (parishcode1) 
est store `var'1
}
esttab st_math st_math1 st_english st_english1 st_total st_total1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("Uwezo weights")

********
*Table S7
********

*standardize Uwezo's imputed variables
gen total_imputed=math_imputed+english_imputed 
foreach var in math_imputed  english_imputed total_imputed {
egen st_`var'=std(`var')
}


*main analysis *using imputed values
foreach var in $outcomes {
reg `var'_imputed treat if age1_7==1, robust cluster (parishcode1) 
est store `var'_imputed
reg `var'_imputed treat $controls if age1_7==1, robust cluster (parishcode1) 
est store `var'_imputed1
}
esttab st_math_imputed st_math_imputed1 st_english_imputed st_english_imputed1 st_total_imputed st_total_imputed1, ///
compress se scalars (N r2) starlevels(* .1 ** .05 *** .01) varlabels(_cons Constant) ///
keep(treat)  replace title("S7 Panel A")

*Panel B: use IPW to reweight for attrition 

*generate inverse probability weights for non-response for each outcome variable
logit missing_math i.age female i.rd if age1_7==1
predict pi_math, pr
gen ipw_math=1
replace ipw_math=1/pi_math if missing_math==1
replace ipw_math =1/(1-pi_math) if missing_math==0

logit missing_english i.age female i.rd if age1_7==1
predict pi_english, pr
gen ipw_english=1
replace ipw_english=1/pi_english if missing_english==1
replace ipw_english =1/(1-pi_english) if missing_english==0

logit missing_obs i.age female i.rd if age1_7==1
predict pi_total, pr
gen ipw_total=1
replace ipw_total=1/pi_total if missing_obs==1
replace ipw_total =1/(1-pi_total) if missing_obs==0

*main analysis*with IPW reweighting 
reg st_math treat if age1_7==1 [pw=ipw_math], robust cluster (parishcode1) 
estimates store reg1
reg st_math treat $controls [pw=ipw_math] if age1_7==1, robust cluster (parishcode1)
estimates store reg2
reg st_english treat [pw=ipw_english] if age1_7==1, robust cluster (parishcode1) 
estimates store reg3
reg st_english treat  $controls [pw=ipw_english] if age1_7==1, robust cluster (parishcode1)
estimates store reg4
reg st_total treat [pw=ipw_total] if age1_7==1, robust cluster (parishcode1) 
estimates store reg5
reg st_total treat $controls [pw=ipw_total] if age1_7==1, robust cluster (parishcode1)
estimates store reg6
esttab reg1 reg2 reg3 reg4 reg5 reg6, compress label se starlevels(* .1 ** .05 *** .01) scalars (N r2) varlabels(_cons Constant) keep(treat)  replace title("Main analysis with imputed outcomes")


